/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.internal.sdk.commands;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
 

/**
 * Deletes all markers from a given Resource set. All markers of the resource and its children are deleted.
 *
 * @author Thomas Kistel
 * @version $Revision: 760 $
 *
 */
public class DeleteMarkersHandler extends AbstractHandler {

    /* (non-Javadoc)
     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelectionChecked(event);
        @SuppressWarnings("unchecked")
        Iterator<Object> iterator = (Iterator<Object>) selection.iterator();
        Set<IResource> affectedResources = new HashSet<IResource>(); 
        try {
            while (iterator.hasNext()) {
                Object object = iterator.next();
                IResource res = adapt(object);
                if(res != null && !affectedResources.contains(res)) {
                    res.deleteMarkers(null, true, IResource.DEPTH_INFINITE);
                    affectedResources.add(res);
                }
            }
        } catch (CoreException e) {
            throw new ExecutionException("Could not validate selected files.", e);
        }
        return null;
    }
    
    IResource adapt(Object obj) {
        if(obj instanceof IResource)
            return (IResource) obj;
        if(obj instanceof IAdaptable) {
            IAdaptable adapt = (IAdaptable) obj;
            IResource res = (IResource) adapt.getAdapter(IResource.class);
            if(res == null) {
                res = (IResource) Platform.getAdapterManager().getAdapter(adapt, IResource.class);
            }
            return res;
        }
        return null;
    }
}
